<?xml version="1.0" encoding="ascii"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>pod.util</title>
  <link rel="stylesheet" href="epydoc.css" type="text/css" />
  <script type="text/javascript" src="epydoc.js"></script>
</head>

<body bgcolor="white" text="black" link="blue" vlink="#204080"
      alink="#204080">
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="pod-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="https://code.google.com/p/py-pod/">py-pod</a></th>
          </tr></table></th>
  </tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="100%">
      <span class="breadcrumbs">
        <a href="pod-module.html">Package&nbsp;pod</a> ::
        Module&nbsp;util
      </span>
    </td>
    <td>
      <table cellpadding="0" cellspacing="0">
        <!-- hide/show private -->
        <tr><td align="right"><span class="options"
            >[<a href="frames.html" target="_top">frames</a
            >]&nbsp;|&nbsp;<a href="pod.util-pysrc.html"
            target="_top">no&nbsp;frames</a>]</span></td></tr>
      </table>
    </td>
  </tr>
</table>
<h1 class="epydoc">Source Code for <a href="pod.util-module.html" onclick="show_private();">Module pod.util</a></h1>
<pre class="py-src">
<a name="L1"></a><tt class="py-lineno">  1</tt>  <tt class="py-line"><tt class="py-docstring">"""</tt> </tt>
<a name="L2"></a><tt class="py-lineno">  2</tt>  <tt class="py-line"><tt class="py-docstring">Operations on matrices and various tools.</tt> </tt>
<a name="L3"></a><tt class="py-lineno">  3</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L4"></a><tt class="py-lineno">  4</tt>  <tt class="py-line"><tt class="py-docstring">@author: Christophe Alexandre &lt;ch.alexandre at bluewin dot ch&gt;</tt> </tt>
<a name="L5"></a><tt class="py-lineno">  5</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L6"></a><tt class="py-lineno">  6</tt>  <tt class="py-line"><tt class="py-docstring">@license:</tt> </tt>
<a name="L7"></a><tt class="py-lineno">  7</tt>  <tt class="py-line"><tt class="py-docstring">Copyright(C) 2010 Christophe Alexandre</tt> </tt>
<a name="L8"></a><tt class="py-lineno">  8</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L9"></a><tt class="py-lineno">  9</tt>  <tt class="py-line"><tt class="py-docstring">This program is free software: you can redistribute it and/or modify</tt> </tt>
<a name="L10"></a><tt class="py-lineno"> 10</tt>  <tt class="py-line"><tt class="py-docstring">it under the terms of the GNU Lesser General Public License as published by</tt> </tt>
<a name="L11"></a><tt class="py-lineno"> 11</tt>  <tt class="py-line"><tt class="py-docstring">the Free Software Foundation, either version 3 of the License, or</tt> </tt>
<a name="L12"></a><tt class="py-lineno"> 12</tt>  <tt class="py-line"><tt class="py-docstring">(at your option) any later version.</tt> </tt>
<a name="L13"></a><tt class="py-lineno"> 13</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L14"></a><tt class="py-lineno"> 14</tt>  <tt class="py-line"><tt class="py-docstring">This program is distributed in the hope that it will be useful,</tt> </tt>
<a name="L15"></a><tt class="py-lineno"> 15</tt>  <tt class="py-line"><tt class="py-docstring">but WITHOUT ANY WARRANTY; without even the implied warranty of</tt> </tt>
<a name="L16"></a><tt class="py-lineno"> 16</tt>  <tt class="py-line"><tt class="py-docstring">MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</tt> </tt>
<a name="L17"></a><tt class="py-lineno"> 17</tt>  <tt class="py-line"><tt class="py-docstring">GNU General Public License for more details.</tt> </tt>
<a name="L18"></a><tt class="py-lineno"> 18</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L19"></a><tt class="py-lineno"> 19</tt>  <tt class="py-line"><tt class="py-docstring">You should have received a copy of the GNU Lesser General Public License</tt> </tt>
<a name="L20"></a><tt class="py-lineno"> 20</tt>  <tt class="py-line"><tt class="py-docstring">along with this program.  If not, see &lt;http://www.gnu.org/licenses/lgpl.txt&gt;.</tt> </tt>
<a name="L21"></a><tt class="py-lineno"> 21</tt>  <tt class="py-line"><tt class="py-docstring">"""</tt> </tt>
<a name="L22"></a><tt class="py-lineno"> 22</tt>  <tt class="py-line"> </tt>
<a name="L23"></a><tt class="py-lineno"> 23</tt>  <tt class="py-line"><tt class="py-keyword">import</tt> <tt class="py-name">math</tt> </tt>
<a name="L24"></a><tt class="py-lineno"> 24</tt>  <tt class="py-line"><tt class="py-keyword">import</tt> <tt class="py-name">logging</tt> </tt>
<a name="L25"></a><tt class="py-lineno"> 25</tt>  <tt class="py-line"> </tt>
<a name="NullHandler"></a><div id="NullHandler-def"><a name="L26"></a><tt class="py-lineno"> 26</tt> <a class="py-toggle" href="#" id="NullHandler-toggle" onclick="return toggle('NullHandler');">-</a><tt class="py-line"><tt class="py-keyword">class</tt> <a class="py-def-name" href="pod.util.NullHandler-class.html">NullHandler</a><tt class="py-op">(</tt><tt class="py-base-class">logging</tt><tt class="py-op">.</tt><tt class="py-base-class">Handler</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="NullHandler-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="NullHandler-expanded"><a name="L27"></a><tt class="py-lineno"> 27</tt>  <tt class="py-line">  <tt class="py-docstring">"""</tt> </tt>
<a name="L28"></a><tt class="py-lineno"> 28</tt>  <tt class="py-line"><tt class="py-docstring">  Null logging in order to avoid warning messages in client applications.</tt> </tt>
<a name="L29"></a><tt class="py-lineno"> 29</tt>  <tt class="py-line"><tt class="py-docstring">  """</tt> </tt>
<a name="NullHandler.emit"></a><div id="NullHandler.emit-def"><a name="L30"></a><tt class="py-lineno"> 30</tt> <a class="py-toggle" href="#" id="NullHandler.emit-toggle" onclick="return toggle('NullHandler.emit');">-</a><tt class="py-line">  <tt class="py-keyword">def</tt> <a class="py-def-name" href="pod.util.NullHandler-class.html#emit">emit</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">record</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="NullHandler.emit-collapsed" style="display:none;" pad="+++" indent="++++++"></div><div id="NullHandler.emit-expanded"><a name="L31"></a><tt class="py-lineno"> 31</tt>  <tt class="py-line">    <tt class="py-keyword">pass</tt> </tt>
</div></div><a name="L32"></a><tt class="py-lineno"> 32</tt>  <tt class="py-line">   </tt>
<a name="L33"></a><tt class="py-lineno"> 33</tt>  <tt class="py-line"><tt id="link-0" class="py-name" targets="Variable pod._h=pod-module.html#_h,Variable pod.linalg._h=pod.linalg-module.html#_h,Variable pod.util._h=pod.util-module.html#_h,Variable pod.vecspace._h=pod.vecspace-module.html#_h"><a title="pod._h
pod.linalg._h
pod.util._h
pod.vecspace._h" class="py-name" href="#" onclick="return doclink('link-0', '_h', 'link-0');">_h</a></tt> <tt class="py-op">=</tt> <tt id="link-1" class="py-name" targets="Class pod.util.NullHandler=pod.util.NullHandler-class.html"><a title="pod.util.NullHandler" class="py-name" href="#" onclick="return doclink('link-1', 'NullHandler', 'link-1');">NullHandler</a></tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
<a name="L34"></a><tt class="py-lineno"> 34</tt>  <tt class="py-line"><tt id="link-2" class="py-name" targets="Variable pod._logger=pod-module.html#_logger,Variable pod.linalg._logger=pod.linalg-module.html#_logger,Variable pod.util._logger=pod.util-module.html#_logger,Variable pod.vecspace._logger=pod.vecspace-module.html#_logger"><a title="pod._logger
pod.linalg._logger
pod.util._logger
pod.vecspace._logger" class="py-name" href="#" onclick="return doclink('link-2', '_logger', 'link-2');">_logger</a></tt> <tt class="py-op">=</tt> <tt class="py-name">logging</tt><tt class="py-op">.</tt><tt class="py-name">getLogger</tt><tt class="py-op">(</tt><tt class="py-string">'util'</tt><tt class="py-op">)</tt> </tt>
<a name="L35"></a><tt class="py-lineno"> 35</tt>  <tt class="py-line"><tt id="link-3" class="py-name"><a title="pod._logger
pod.linalg._logger
pod.util._logger
pod.vecspace._logger" class="py-name" href="#" onclick="return doclink('link-3', '_logger', 'link-2');">_logger</a></tt><tt class="py-op">.</tt><tt class="py-name">addHandler</tt><tt class="py-op">(</tt><tt id="link-4" class="py-name"><a title="pod._h
pod.linalg._h
pod.util._h
pod.vecspace._h" class="py-name" href="#" onclick="return doclink('link-4', '_h', 'link-0');">_h</a></tt><tt class="py-op">)</tt> </tt>
<a name="L36"></a><tt class="py-lineno"> 36</tt>  <tt class="py-line"> </tt>
<a name="numbering"></a><div id="numbering-def"><a name="L37"></a><tt class="py-lineno"> 37</tt> <a class="py-toggle" href="#" id="numbering-toggle" onclick="return toggle('numbering');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="pod.util-module.html#numbering">numbering</a><tt class="py-op">(</tt><tt class="py-param">v</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="numbering-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="numbering-expanded"><a name="L38"></a><tt class="py-lineno"> 38</tt>  <tt class="py-line">  <tt class="py-docstring">""" Maps every element of to its position."""</tt> </tt>
<a name="L39"></a><tt class="py-lineno"> 39</tt>  <tt class="py-line">  <tt class="py-keyword">return</tt> <tt class="py-name">zip</tt><tt class="py-op">(</tt><tt class="py-name">range</tt><tt class="py-op">(</tt><tt class="py-name">len</tt><tt class="py-op">(</tt><tt class="py-name">v</tt><tt class="py-op">)</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-name">v</tt><tt class="py-op">)</tt> </tt>
</div><a name="L40"></a><tt class="py-lineno"> 40</tt>  <tt class="py-line"> </tt>
<a name="prod_scalar"></a><div id="prod_scalar-def"><a name="L41"></a><tt class="py-lineno"> 41</tt> <a class="py-toggle" href="#" id="prod_scalar-toggle" onclick="return toggle('prod_scalar');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="pod.util-module.html#prod_scalar">prod_scalar</a><tt class="py-op">(</tt><tt class="py-param">v1</tt><tt class="py-op">,</tt> <tt class="py-param">v2</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="prod_scalar-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="prod_scalar-expanded"><a name="L42"></a><tt class="py-lineno"> 42</tt>  <tt class="py-line">  <tt class="py-keyword">assert</tt> <tt class="py-name">len</tt><tt class="py-op">(</tt><tt class="py-name">v1</tt><tt class="py-op">)</tt> <tt class="py-op">==</tt> <tt class="py-name">len</tt><tt class="py-op">(</tt><tt class="py-name">v2</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-string">'input vectors must be of the same size'</tt> </tt>
<a name="L43"></a><tt class="py-lineno"> 43</tt>  <tt class="py-line">  <tt class="py-name">prod</tt> <tt class="py-op">=</tt> <tt class="py-name">map</tt><tt class="py-op">(</tt><tt class="py-keyword">lambda</tt> <tt class="py-name">x</tt><tt class="py-op">:</tt> <tt class="py-name">x</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt> <tt class="py-op">*</tt> <tt class="py-name">x</tt><tt class="py-op">[</tt><tt class="py-number">1</tt><tt class="py-op">]</tt><tt class="py-op">,</tt> <tt class="py-name">zip</tt><tt class="py-op">(</tt><tt class="py-name">v1</tt><tt class="py-op">,</tt> <tt class="py-name">v2</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> </tt>
<a name="L44"></a><tt class="py-lineno"> 44</tt>  <tt class="py-line">  <tt class="py-keyword">return</tt> <tt class="py-name">sum</tt><tt class="py-op">(</tt><tt class="py-name">prod</tt><tt class="py-op">)</tt> </tt>
</div><a name="L45"></a><tt class="py-lineno"> 45</tt>  <tt class="py-line"> </tt>
<a name="norm"></a><div id="norm-def"><a name="L46"></a><tt class="py-lineno"> 46</tt> <a class="py-toggle" href="#" id="norm-toggle" onclick="return toggle('norm');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="pod.util-module.html#norm">norm</a><tt class="py-op">(</tt><tt class="py-param">v</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="norm-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="norm-expanded"><a name="L47"></a><tt class="py-lineno"> 47</tt>  <tt class="py-line">  <tt class="py-keyword">return</tt> <tt class="py-name">math</tt><tt class="py-op">.</tt><tt class="py-name">sqrt</tt><tt class="py-op">(</tt><tt id="link-5" class="py-name" targets="Function pod.util.prod_scalar()=pod.util-module.html#prod_scalar"><a title="pod.util.prod_scalar" class="py-name" href="#" onclick="return doclink('link-5', 'prod_scalar', 'link-5');">prod_scalar</a></tt><tt class="py-op">(</tt><tt class="py-name">v</tt><tt class="py-op">,</tt> <tt class="py-name">v</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> </tt>
</div><a name="L48"></a><tt class="py-lineno"> 48</tt>  <tt class="py-line"> </tt>
<a name="gauss_jordan"></a><div id="gauss_jordan-def"><a name="L49"></a><tt class="py-lineno"> 49</tt> <a class="py-toggle" href="#" id="gauss_jordan-toggle" onclick="return toggle('gauss_jordan');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="pod.util-module.html#gauss_jordan">gauss_jordan</a><tt class="py-op">(</tt><tt class="py-param">m</tt><tt class="py-op">,</tt> <tt class="py-param">eps</tt><tt class="py-op">=</tt><tt class="py-number">1E-10</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="gauss_jordan-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="gauss_jordan-expanded"><a name="L50"></a><tt class="py-lineno"> 50</tt>  <tt class="py-line">  <tt class="py-docstring">"""</tt> </tt>
<a name="L51"></a><tt class="py-lineno"> 51</tt>  <tt class="py-line"><tt class="py-docstring">  Puts given matrix (2D array) into the Reduced Row Echelon Form.</tt> </tt>
<a name="L52"></a><tt class="py-lineno"> 52</tt>  <tt class="py-line"><tt class="py-docstring">  </tt> </tt>
<a name="L53"></a><tt class="py-lineno"> 53</tt>  <tt class="py-line"><tt class="py-docstring">  NOTE: make sure all the matrix items support fractions! Int matrix will NOT work!</tt> </tt>
<a name="L54"></a><tt class="py-lineno"> 54</tt>  <tt class="py-line"><tt class="py-docstring">  Written by Jarno Elonen in April 2005, released into Public Domain</tt> </tt>
<a name="L55"></a><tt class="py-lineno"> 55</tt>  <tt class="py-line"><tt class="py-docstring">     </tt> </tt>
<a name="L56"></a><tt class="py-lineno"> 56</tt>  <tt class="py-line"><tt class="py-docstring">  @return: True if successful, False if 'm' is singular.</tt> </tt>
<a name="L57"></a><tt class="py-lineno"> 57</tt>  <tt class="py-line"><tt class="py-docstring">  """</tt> </tt>
<a name="L58"></a><tt class="py-lineno"> 58</tt>  <tt class="py-line">  <tt class="py-op">(</tt><tt class="py-name">h</tt><tt class="py-op">,</tt> <tt class="py-name">w</tt><tt class="py-op">)</tt> <tt class="py-op">=</tt> <tt class="py-op">(</tt><tt class="py-name">len</tt><tt class="py-op">(</tt><tt class="py-name">m</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-name">len</tt><tt class="py-op">(</tt><tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> </tt>
<a name="L59"></a><tt class="py-lineno"> 59</tt>  <tt class="py-line">  <tt class="py-keyword">for</tt> <tt class="py-name">y</tt> <tt class="py-keyword">in</tt> <tt class="py-name">range</tt><tt class="py-op">(</tt><tt class="py-number">0</tt><tt class="py-op">,</tt><tt class="py-name">h</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L60"></a><tt class="py-lineno"> 60</tt>  <tt class="py-line">    <tt class="py-name">maxrow</tt> <tt class="py-op">=</tt> <tt class="py-name">y</tt> </tt>
<a name="L61"></a><tt class="py-lineno"> 61</tt>  <tt class="py-line">    <tt class="py-keyword">for</tt> <tt class="py-name">y2</tt> <tt class="py-keyword">in</tt> <tt class="py-name">range</tt><tt class="py-op">(</tt><tt class="py-name">y</tt><tt class="py-op">+</tt><tt class="py-number">1</tt><tt class="py-op">,</tt> <tt class="py-name">h</tt><tt class="py-op">)</tt><tt class="py-op">:</tt>    <tt class="py-comment"># Find max pivot</tt> </tt>
<a name="L62"></a><tt class="py-lineno"> 62</tt>  <tt class="py-line">      <tt class="py-keyword">if</tt> <tt class="py-name">abs</tt><tt class="py-op">(</tt><tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">y2</tt><tt class="py-op">]</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt><tt class="py-op">)</tt> <tt class="py-op">&gt;</tt> <tt class="py-name">abs</tt><tt class="py-op">(</tt><tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">maxrow</tt><tt class="py-op">]</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L63"></a><tt class="py-lineno"> 63</tt>  <tt class="py-line">        <tt class="py-name">maxrow</tt> <tt class="py-op">=</tt> <tt class="py-name">y2</tt> </tt>
<a name="L64"></a><tt class="py-lineno"> 64</tt>  <tt class="py-line">    <tt class="py-op">(</tt><tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt><tt class="py-op">,</tt> <tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">maxrow</tt><tt class="py-op">]</tt><tt class="py-op">)</tt> <tt class="py-op">=</tt> <tt class="py-op">(</tt><tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">maxrow</tt><tt class="py-op">]</tt><tt class="py-op">,</tt> <tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt><tt class="py-op">)</tt> </tt>
<a name="L65"></a><tt class="py-lineno"> 65</tt>  <tt class="py-line">    <tt class="py-keyword">if</tt> <tt class="py-name">abs</tt><tt class="py-op">(</tt><tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt><tt class="py-op">)</tt> <tt class="py-op">&lt;=</tt> <tt class="py-name">eps</tt><tt class="py-op">:</tt>     <tt class="py-comment"># Singular?</tt> </tt>
<a name="L66"></a><tt class="py-lineno"> 66</tt>  <tt class="py-line">      <tt class="py-keyword">return</tt> <tt class="py-name">False</tt> </tt>
<a name="L67"></a><tt class="py-lineno"> 67</tt>  <tt class="py-line">    <tt class="py-keyword">for</tt> <tt class="py-name">y2</tt> <tt class="py-keyword">in</tt> <tt class="py-name">range</tt><tt class="py-op">(</tt><tt class="py-name">y</tt><tt class="py-op">+</tt><tt class="py-number">1</tt><tt class="py-op">,</tt> <tt class="py-name">h</tt><tt class="py-op">)</tt><tt class="py-op">:</tt>    <tt class="py-comment"># Eliminate column y</tt> </tt>
<a name="L68"></a><tt class="py-lineno"> 68</tt>  <tt class="py-line">      <tt class="py-name">c</tt> <tt class="py-op">=</tt> <tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">y2</tt><tt class="py-op">]</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt> <tt class="py-op">/</tt> <tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt> </tt>
<a name="L69"></a><tt class="py-lineno"> 69</tt>  <tt class="py-line">      <tt class="py-keyword">for</tt> <tt class="py-name">x</tt> <tt class="py-keyword">in</tt> <tt class="py-name">range</tt><tt class="py-op">(</tt><tt class="py-name">y</tt><tt class="py-op">,</tt> <tt class="py-name">w</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L70"></a><tt class="py-lineno"> 70</tt>  <tt class="py-line">        <tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">y2</tt><tt class="py-op">]</tt><tt class="py-op">[</tt><tt class="py-name">x</tt><tt class="py-op">]</tt> <tt class="py-op">-=</tt> <tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt><tt class="py-op">[</tt><tt class="py-name">x</tt><tt class="py-op">]</tt> <tt class="py-op">*</tt> <tt class="py-name">c</tt> </tt>
<a name="L71"></a><tt class="py-lineno"> 71</tt>  <tt class="py-line">  <tt class="py-keyword">for</tt> <tt class="py-name">y</tt> <tt class="py-keyword">in</tt> <tt class="py-name">range</tt><tt class="py-op">(</tt><tt class="py-name">h</tt><tt class="py-op">-</tt><tt class="py-number">1</tt><tt class="py-op">,</tt> <tt class="py-number">0</tt><tt class="py-op">-</tt><tt class="py-number">1</tt><tt class="py-op">,</tt> <tt class="py-op">-</tt><tt class="py-number">1</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> <tt class="py-comment"># Backsubstitute</tt> </tt>
<a name="L72"></a><tt class="py-lineno"> 72</tt>  <tt class="py-line">    <tt class="py-name">c</tt>  <tt class="py-op">=</tt> <tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt> </tt>
<a name="L73"></a><tt class="py-lineno"> 73</tt>  <tt class="py-line">    <tt class="py-keyword">for</tt> <tt class="py-name">y2</tt> <tt class="py-keyword">in</tt> <tt class="py-name">range</tt><tt class="py-op">(</tt><tt class="py-number">0</tt><tt class="py-op">,</tt><tt class="py-name">y</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L74"></a><tt class="py-lineno"> 74</tt>  <tt class="py-line">      <tt class="py-keyword">for</tt> <tt class="py-name">x</tt> <tt class="py-keyword">in</tt> <tt class="py-name">range</tt><tt class="py-op">(</tt><tt class="py-name">w</tt><tt class="py-op">-</tt><tt class="py-number">1</tt><tt class="py-op">,</tt> <tt class="py-name">y</tt><tt class="py-op">-</tt><tt class="py-number">1</tt><tt class="py-op">,</tt> <tt class="py-op">-</tt><tt class="py-number">1</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L75"></a><tt class="py-lineno"> 75</tt>  <tt class="py-line">        <tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">y2</tt><tt class="py-op">]</tt><tt class="py-op">[</tt><tt class="py-name">x</tt><tt class="py-op">]</tt> <tt class="py-op">-=</tt>  <tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt><tt class="py-op">[</tt><tt class="py-name">x</tt><tt class="py-op">]</tt> <tt class="py-op">*</tt> <tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">y2</tt><tt class="py-op">]</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt> <tt class="py-op">/</tt> <tt class="py-name">c</tt> </tt>
<a name="L76"></a><tt class="py-lineno"> 76</tt>  <tt class="py-line">    <tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt> <tt class="py-op">/=</tt> <tt class="py-name">c</tt> </tt>
<a name="L77"></a><tt class="py-lineno"> 77</tt>  <tt class="py-line">    <tt class="py-keyword">for</tt> <tt class="py-name">x</tt> <tt class="py-keyword">in</tt> <tt class="py-name">range</tt><tt class="py-op">(</tt><tt class="py-name">h</tt><tt class="py-op">,</tt> <tt class="py-name">w</tt><tt class="py-op">)</tt><tt class="py-op">:</tt>       <tt class="py-comment"># Normalize row y</tt> </tt>
<a name="L78"></a><tt class="py-lineno"> 78</tt>  <tt class="py-line">      <tt class="py-name">m</tt><tt class="py-op">[</tt><tt class="py-name">y</tt><tt class="py-op">]</tt><tt class="py-op">[</tt><tt class="py-name">x</tt><tt class="py-op">]</tt> <tt class="py-op">/=</tt> <tt class="py-name">c</tt> </tt>
<a name="L79"></a><tt class="py-lineno"> 79</tt>  <tt class="py-line">  <tt class="py-keyword">return</tt> <tt class="py-name">True</tt> </tt>
</div><a name="L80"></a><tt class="py-lineno"> 80</tt>  <tt class="py-line"> </tt>
<a name="L81"></a><tt class="py-lineno"> 81</tt>  <tt class="py-line"><tt class="py-comment"># Auxiliary functions contribution by Eric Atienza</tt> </tt>
<a name="L82"></a><tt class="py-lineno"> 82</tt>  <tt class="py-line"><tt class="py-comment"></tt> </tt>
<a name="system_solve"></a><div id="system_solve-def"><a name="L83"></a><tt class="py-lineno"> 83</tt> <a class="py-toggle" href="#" id="system_solve-toggle" onclick="return toggle('system_solve');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="pod.util-module.html#system_solve">system_solve</a><tt class="py-op">(</tt><tt class="py-param">M</tt><tt class="py-op">,</tt> <tt class="py-param">b</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="system_solve-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="system_solve-expanded"><a name="L84"></a><tt class="py-lineno"> 84</tt>  <tt class="py-line">  <tt class="py-docstring">"""</tt> </tt>
<a name="L85"></a><tt class="py-lineno"> 85</tt>  <tt class="py-line"><tt class="py-docstring">  solves M*x = b</tt> </tt>
<a name="L86"></a><tt class="py-lineno"> 86</tt>  <tt class="py-line"><tt class="py-docstring">  @param M: a matrix in the form of a list of list</tt> </tt>
<a name="L87"></a><tt class="py-lineno"> 87</tt>  <tt class="py-line"><tt class="py-docstring">  @param b: a vector in the form of a simple list of scalars</tt> </tt>
<a name="L88"></a><tt class="py-lineno"> 88</tt>  <tt class="py-line"><tt class="py-docstring">  @return: vector x so that M*x = b</tt> </tt>
<a name="L89"></a><tt class="py-lineno"> 89</tt>  <tt class="py-line"><tt class="py-docstring">  """</tt> </tt>
<a name="L90"></a><tt class="py-lineno"> 90</tt>  <tt class="py-line">  <tt class="py-name">m2</tt> <tt class="py-op">=</tt> <tt class="py-op">[</tt><tt class="py-name">row</tt><tt class="py-op">[</tt><tt class="py-op">:</tt><tt class="py-op">]</tt><tt class="py-op">+</tt><tt class="py-op">[</tt><tt class="py-name">right</tt><tt class="py-op">]</tt> <tt class="py-keyword">for</tt> <tt class="py-name">row</tt><tt class="py-op">,</tt><tt class="py-name">right</tt> <tt class="py-keyword">in</tt> <tt class="py-name">zip</tt><tt class="py-op">(</tt><tt class="py-name">M</tt><tt class="py-op">,</tt><tt class="py-name">b</tt><tt class="py-op">)</tt> <tt class="py-op">]</tt> </tt>
<a name="L91"></a><tt class="py-lineno"> 91</tt>  <tt class="py-line">  <tt class="py-keyword">return</tt> <tt class="py-op">[</tt><tt class="py-name">row</tt><tt class="py-op">[</tt><tt class="py-op">-</tt><tt class="py-number">1</tt><tt class="py-op">]</tt> <tt class="py-keyword">for</tt> <tt class="py-name">row</tt> <tt class="py-keyword">in</tt> <tt class="py-name">m2</tt><tt class="py-op">]</tt> <tt class="py-keyword">if</tt> <tt id="link-6" class="py-name" targets="Function pod.util.gauss_jordan()=pod.util-module.html#gauss_jordan"><a title="pod.util.gauss_jordan" class="py-name" href="#" onclick="return doclink('link-6', 'gauss_jordan', 'link-6');">gauss_jordan</a></tt><tt class="py-op">(</tt><tt class="py-name">m2</tt><tt class="py-op">)</tt> <tt class="py-keyword">else</tt> <tt class="py-name">None</tt> </tt>
</div><a name="L92"></a><tt class="py-lineno"> 92</tt>  <tt class="py-line"> </tt>
<a name="mat_inverse"></a><div id="mat_inverse-def"><a name="L93"></a><tt class="py-lineno"> 93</tt> <a class="py-toggle" href="#" id="mat_inverse-toggle" onclick="return toggle('mat_inverse');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="pod.util-module.html#mat_inverse">mat_inverse</a><tt class="py-op">(</tt><tt class="py-param">M</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="mat_inverse-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="mat_inverse-expanded"><a name="L94"></a><tt class="py-lineno"> 94</tt>  <tt class="py-line">  <tt class="py-docstring">"""</tt> </tt>
<a name="L95"></a><tt class="py-lineno"> 95</tt>  <tt class="py-line"><tt class="py-docstring">  @return: the inverse of the matrix M</tt> </tt>
<a name="L96"></a><tt class="py-lineno"> 96</tt>  <tt class="py-line"><tt class="py-docstring">  """</tt> </tt>
<a name="L97"></a><tt class="py-lineno"> 97</tt>  <tt class="py-line">  <tt class="py-comment">#clone the matrix and append the identity matrix</tt> </tt>
<a name="L98"></a><tt class="py-lineno"> 98</tt>  <tt class="py-line"><tt class="py-comment"></tt>  <tt class="py-comment"># [int(i==j) for j in range_M] is nothing but the i(th row of the identity matrix</tt> </tt>
<a name="L99"></a><tt class="py-lineno"> 99</tt>  <tt class="py-line"><tt class="py-comment"></tt>  <tt class="py-name">m2</tt> <tt class="py-op">=</tt> <tt class="py-op">[</tt><tt class="py-name">row</tt><tt class="py-op">[</tt><tt class="py-op">:</tt><tt class="py-op">]</tt><tt class="py-op">+</tt><tt class="py-op">[</tt><tt class="py-name">int</tt><tt class="py-op">(</tt><tt class="py-name">i</tt><tt class="py-op">==</tt><tt class="py-name">j</tt><tt class="py-op">)</tt> <tt class="py-keyword">for</tt> <tt class="py-name">j</tt> <tt class="py-keyword">in</tt> <tt class="py-name">range</tt><tt class="py-op">(</tt><tt class="py-name">len</tt><tt class="py-op">(</tt><tt class="py-name">M</tt><tt class="py-op">)</tt> <tt class="py-op">)</tt><tt class="py-op">]</tt> <tt class="py-keyword">for</tt> <tt class="py-name">i</tt><tt class="py-op">,</tt><tt class="py-name">row</tt> <tt class="py-keyword">in</tt> <tt class="py-name">enumerate</tt><tt class="py-op">(</tt><tt class="py-name">M</tt><tt class="py-op">)</tt> <tt class="py-op">]</tt> </tt>
<a name="L100"></a><tt class="py-lineno">100</tt>  <tt class="py-line">  <tt class="py-comment"># extract the appended matrix (kind of m2[m:,...]</tt> </tt>
<a name="L101"></a><tt class="py-lineno">101</tt>  <tt class="py-line"><tt class="py-comment"></tt>  <tt class="py-keyword">return</tt> <tt class="py-op">[</tt><tt class="py-name">row</tt><tt class="py-op">[</tt><tt class="py-name">len</tt><tt class="py-op">(</tt><tt class="py-name">M</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt><tt class="py-op">)</tt><tt class="py-op">:</tt><tt class="py-op">]</tt> <tt class="py-keyword">for</tt> <tt class="py-name">row</tt> <tt class="py-keyword">in</tt> <tt class="py-name">m2</tt><tt class="py-op">]</tt> <tt class="py-keyword">if</tt> <tt id="link-7" class="py-name"><a title="pod.util.gauss_jordan" class="py-name" href="#" onclick="return doclink('link-7', 'gauss_jordan', 'link-6');">gauss_jordan</a></tt><tt class="py-op">(</tt><tt class="py-name">m2</tt><tt class="py-op">)</tt> <tt class="py-keyword">else</tt> <tt class="py-name">None</tt> </tt>
</div><a name="L102"></a><tt class="py-lineno">102</tt>  <tt class="py-line"> </tt>
<a name="zeros"></a><div id="zeros-def"><a name="L103"></a><tt class="py-lineno">103</tt> <a class="py-toggle" href="#" id="zeros-toggle" onclick="return toggle('zeros');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="pod.util-module.html#zeros">zeros</a><tt class="py-op">(</tt><tt class="py-param">size</tt><tt class="py-op">,</tt> <tt class="py-param">zero</tt><tt class="py-op">=</tt><tt class="py-number">0</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="zeros-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="zeros-expanded"><a name="L104"></a><tt class="py-lineno">104</tt>  <tt class="py-line">  <tt class="py-docstring">"""</tt> </tt>
<a name="L105"></a><tt class="py-lineno">105</tt>  <tt class="py-line"><tt class="py-docstring">  @param size: a tuple containing dimensions of the matrix</tt> </tt>
<a name="L106"></a><tt class="py-lineno">106</tt>  <tt class="py-line"><tt class="py-docstring">  @param zero: the value to use to fill the matrix (zero by default)</tt> </tt>
<a name="L107"></a><tt class="py-lineno">107</tt>  <tt class="py-line"><tt class="py-docstring">  @return: matrix of dimension size</tt> </tt>
<a name="L108"></a><tt class="py-lineno">108</tt>  <tt class="py-line"><tt class="py-docstring">  """</tt> </tt>
<a name="L109"></a><tt class="py-lineno">109</tt>  <tt class="py-line">  <tt class="py-keyword">return</tt> <tt class="py-op">[</tt><tt id="link-8" class="py-name" targets="Function pod.util.zeros()=pod.util-module.html#zeros"><a title="pod.util.zeros" class="py-name" href="#" onclick="return doclink('link-8', 'zeros', 'link-8');">zeros</a></tt><tt class="py-op">(</tt><tt class="py-name">s</tt><tt class="py-op">[</tt><tt class="py-number">1</tt><tt class="py-op">:</tt><tt class="py-op">]</tt> <tt class="py-op">)</tt> <tt class="py-keyword">for</tt> <tt class="py-name">i</tt> <tt class="py-keyword">in</tt> <tt class="py-name">range</tt><tt class="py-op">(</tt><tt class="py-name">s</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt> <tt class="py-op">)</tt> <tt class="py-op">]</tt> <tt class="py-keyword">if</tt> <tt class="py-keyword">not</tt> <tt class="py-name">len</tt><tt class="py-op">(</tt><tt class="py-name">s</tt><tt class="py-op">)</tt> <tt class="py-keyword">else</tt> <tt id="link-9" class="py-name" targets="Function pod.linalg.zero()=pod.linalg-module.html#zero"><a title="pod.linalg.zero" class="py-name" href="#" onclick="return doclink('link-9', 'zero', 'link-9');">zero</a></tt> </tt>
</div><a name="L110"></a><tt class="py-lineno">110</tt>  <tt class="py-line"> </tt><script type="text/javascript">
<!--
expandto(location.href);
// -->
</script>
</pre>
<br />
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="pod-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="https://code.google.com/p/py-pod/">py-pod</a></th>
          </tr></table></th>
  </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
  <tr>
    <td align="left" class="footer">
    Generated by Epydoc 3.0.1 on Thu Aug 04 16:36:46 2011
    </td>
    <td align="right" class="footer">
      <a target="mainFrame" href="http://epydoc.sourceforge.net"
        >http://epydoc.sourceforge.net</a>
    </td>
  </tr>
</table>

<script type="text/javascript">
  <!--
  // Private objects are initially displayed (because if
  // javascript is turned off then we want them to be
  // visible); but by default, we want to hide them.  So hide
  // them unless we have a cookie that says to show them.
  checkCookie();
  // -->
</script>
</body>
</html>
